package com.ehome.user.Activity.Login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ehome.user.Module.user;
import com.ehome.user.R;
import com.ehome.user.Utils.TimeCount;
import com.ehome.user.Utils.Tools;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;


/**
 * Created by Administrator on 2018/3/8 0008.
 */

public class ForgetPasswodActivity extends AppCompatActivity  {

    @Bind(R.id.bt_next)
    Button button;
    @Bind(R.id.btn_send_reg)
    TextView btn_send_reg;
    @Bind(R.id.edit_phone_reg)
    EditText edit_phone_reg;
    @Bind(R.id.edit_checknum_reg)
    EditText edit_checknum_reg;
    @Bind(R.id.tv_base_title)
    TextView tv_base_title;
    private TimeCount time;
    private String pastag;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpsw);
        ButterKnife.bind(this);
        pastag=getIntent().getStringExtra("pastag");
        time = new TimeCount(ForgetPasswodActivity.this, 60000, 1000, btn_send_reg);
        if(pastag.equals("1")){
            tv_base_title.setText("找回密码");
        }else if(pastag.equals("2")){
            tv_base_title.setText("注册");
        }
    }
    @OnClick({R.id.btn_send_reg,R.id.bt_next,R.id.img_base_back})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.btn_send_reg:
                if (TextUtils.isEmpty(edit_phone_reg.getText().toString())) {
                    Toast.makeText(ForgetPasswodActivity.this,"请先输入手机号",Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (!Tools.isMobileNumber(edit_phone_reg.getText().toString())) {
                        Toast.makeText(ForgetPasswodActivity.this,"电话号码格式不正确",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if(pastag.equals("1")){//忘记密码
                    BmobQuery<user> query = new BmobQuery<user>();
                    query.addWhereEqualTo("phone",edit_phone_reg.getText().toString());
                    query.findObjects(new FindListener<user>() {
                        @Override
                        public void done(List<user> list, BmobException e) {
                            if(e==null){
                                if(list.size()==0){
                                    Toast.makeText(ForgetPasswodActivity.this,"未注册",Toast.LENGTH_SHORT).show();
                                }else {
                                    BmobSMS.requestSMSCode(edit_phone_reg.getText().toString(), "易家宝", new QueryListener<Integer>() {
                                        @Override
                                        public void done(Integer integer, BmobException e) {
                                            if(e==null){
                                                Toast.makeText(ForgetPasswodActivity.this,"发送短信成功",Toast.LENGTH_SHORT).show();
                                                time.start();
                                            }else {
                                                Toast.makeText(ForgetPasswodActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }
                            }else {
                                Toast.makeText(ForgetPasswodActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else if(pastag.equals("2")){//注册账号
                    BmobQuery<user> query = new BmobQuery<user>();
                    query.addWhereEqualTo("phone",edit_phone_reg.getText().toString());
                    query.findObjects(new FindListener<user>() {
                        @Override
                        public void done(List<user> list, BmobException e) {
                            if(e==null){
                                if(list.size()==0){
                                    BmobSMS.requestSMSCode(edit_phone_reg.getText().toString(), "易家宝", new QueryListener<Integer>() {
                                        @Override
                                        public void done(Integer integer, BmobException e) {
                                            if(e==null){
                                                Toast.makeText(ForgetPasswodActivity.this,"发送短信成功",Toast.LENGTH_SHORT).show();
                                                time.start();
                                            }else {
                                                Toast.makeText(ForgetPasswodActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }else {
                                    Toast.makeText(ForgetPasswodActivity.this,"该手机号已注册",Toast.LENGTH_SHORT).show();
                                }
                            }else {
                                Toast.makeText(ForgetPasswodActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
            case R.id.bt_next:

                if (TextUtils.isEmpty(edit_phone_reg.getText().toString())) {
                    Toast.makeText(ForgetPasswodActivity.this,"请先输入手机号",Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (!Tools.isMobileNumber(edit_phone_reg.getText().toString())) {
                        Toast.makeText(ForgetPasswodActivity.this,"电话号码格式不正确",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if(TextUtils.isEmpty(edit_checknum_reg.getText().toString())){
                    Toast.makeText(ForgetPasswodActivity.this,"请先输入验证码",Toast.LENGTH_SHORT).show();
                    return;
                }

                BmobSMS.verifySmsCode(edit_phone_reg.getText().toString(), edit_checknum_reg.getText().toString(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(ForgetPasswodActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent();
                            intent.putExtra("phone",edit_phone_reg.getText().toString());
                            intent.setClass(ForgetPasswodActivity.this,PasswordActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(ForgetPasswodActivity.this,"验证失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.img_base_back:
                finish();
                break;
        }
    }
}
